package com.meals.system.service.impl;

import com.meals.system.api.system.vo.SecondLevelCategoryVO;
import com.meals.system.api.system.vo.ThirdLevelCategoryVO;
import com.meals.system.api.system.vo.WhitesMealIndexCategoryVO;
import com.meals.system.common.Constants;
import com.meals.system.common.ServiceResultEnum;
import com.meals.system.common.WhitesMealSellerCategoryLevelEnum;
import com.meals.system.dao.SellerCategoryMapper;
import com.meals.system.entity.SystemSellerCategory;
import com.meals.system.service.WhitesMealSellerCategoryService;
import com.meals.system.util.BeanUtil;
import com.meals.system.util.PageQueryUtil;
import com.meals.system.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.groupingBy;

/**
 * Description:
 * Parm:
 * return:
 * Author: Chen
 * Date: 2021/12/10
 */
@Service
public class WhitesMealSellerCategoryServiceImpl implements WhitesMealSellerCategoryService {

    @Autowired
    private SellerCategoryMapper sellerCategoryMapper;


    @Override
    public String saveCategory(SystemSellerCategory sellerCategory) {
        SystemSellerCategory temp = sellerCategoryMapper.selectByLevelAndName(sellerCategory.getCategoryLevel(), sellerCategory.getCategoryName());
        if (temp != null){
            return ServiceResultEnum.SAME_CATEGORY_EXIST.getResult();
        }
        if (sellerCategoryMapper.insertSelective(sellerCategory) > 0){
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();

    }

    @Override
    public String updateSellerCategory(SystemSellerCategory sellerCategory) {
        SystemSellerCategory temp = sellerCategoryMapper.selectByPrimaryKey(sellerCategory.getCategoryId());
        if (temp == null) {
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        SystemSellerCategory sellerCategory1 = sellerCategoryMapper.selectByLevelAndName(sellerCategory.getCategoryLevel(), sellerCategory.getCategoryName());
        // 不同id 其他相同不批准修改
        if (sellerCategory1 != null && !sellerCategory1.getCategoryId().equals(sellerCategory.getCategoryId())){
            return ServiceResultEnum.SAME_CATEGORY_EXIST.getResult();
        }
        sellerCategory.setUpdateTime(new Date());
        if (sellerCategoryMapper.updateByPrimaryKeySelective(sellerCategory) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }



    @Override
    public SystemSellerCategory getSellerCategoryById(Long id) {
        return sellerCategoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public Boolean deleteBatch(Long[] ids) {
        if (ids.length < 1){
            return false;
        }
        // 删除数据
        return sellerCategoryMapper.deleteBatch(ids) > 0;
    }

    @Override
    public PageResult getCategoryPage(PageQueryUtil pageUtil) {
        List<SystemSellerCategory> sellerCategories = sellerCategoryMapper.findSellerCategoryList(pageUtil);
//        System.out.println("sellerCategories = " + sellerCategories);
        int total = sellerCategoryMapper.getTotalSellerCategories(pageUtil);
        PageResult pageResult = new PageResult(sellerCategories, total, pageUtil.getLimit(), pageUtil.getPage());
        return pageResult;
    }

    @Override
    public List<SystemSellerCategory> selectByLevelAndParentIdsAndNumber(List<Long> parentIds, int categoryLevel) {
        // 0代表查所有
        return sellerCategoryMapper.selectByLevelAndParentIdsAndNumber(parentIds, categoryLevel, 0);
    }

    @Override
    public List<WhitesMealIndexCategoryVO> getCategoriesForIndex(){
        List<WhitesMealIndexCategoryVO> whitesMealIndexCategoryVOS = new ArrayList<>();

        // 获取一级分类的固定数量的数据
        List<SystemSellerCategory> firstLevelCategories = sellerCategoryMapper.selectByLevelAndParentIdsAndNumber(Collections.singletonList(0L), WhitesMealSellerCategoryLevelEnum.LEVEL_ONE.getLevel(), Constants.INDEX_CATEGORY_NUMBER);
        if (!CollectionUtils.isEmpty(firstLevelCategories)){
            List<Long> firstLevelCategoryIds = firstLevelCategories.stream().map(SystemSellerCategory::getCategoryId).collect(Collectors.toList());

            // 获取二级分类的数据 number<=0的时候拿全部
            List<SystemSellerCategory> secondLevelCategories = sellerCategoryMapper.selectByLevelAndParentIdsAndNumber(firstLevelCategoryIds, WhitesMealSellerCategoryLevelEnum.LEVEL_TWO.getLevel(), 0);
            if (!CollectionUtils.isEmpty(secondLevelCategories)) {
                List<Long> secondLevelCategoryIds = secondLevelCategories.stream().map(SystemSellerCategory::getCategoryId).collect(Collectors.toList());

                // 获取三级分类的数据
                List<SystemSellerCategory> thirdLevelCategories = sellerCategoryMapper.selectByLevelAndParentIdsAndNumber(secondLevelCategoryIds, WhitesMealSellerCategoryLevelEnum.LEVEL_THREE.getLevel(), 0);
                if (!CollectionUtils.isEmpty(thirdLevelCategories)){
                    // 根据parentId 将 thirdLevelCategories 分组 key是parentId
                    Map<Long, List<SystemSellerCategory>> thirdLevelCategoryMap = thirdLevelCategories.stream().collect(groupingBy(SystemSellerCategory::getParentId));
                    List<SecondLevelCategoryVO> secondLevelCategoryVOS = new ArrayList<>();

                    // 处理二级分类
                    for (SystemSellerCategory secondLevelCategory : secondLevelCategories){
                        SecondLevelCategoryVO secondLevelCategoryVO = new SecondLevelCategoryVO();
                        // copyProperties(source,target)
                        BeanUtil.copyProperties(secondLevelCategory, secondLevelCategoryVO);
                        // 如果该二级分类下有数据则放入 secondLevelCategoryVOS 对象中
                        if (thirdLevelCategoryMap.containsKey(secondLevelCategory.getCategoryId())){
                            // 根据二级分类的id取出thirdLevelCategoryMap分组中的三级分类list
                            // 关键: 二级的id对应三级的parentId
                            List<SystemSellerCategory> tempSellerCategories = thirdLevelCategoryMap.get(secondLevelCategory.getCategoryId());
                            secondLevelCategoryVO.setThirdLevelCategoryVOS(BeanUtil.copyList(tempSellerCategories, ThirdLevelCategoryVO.class));
                            secondLevelCategoryVOS.add(secondLevelCategoryVO);

                        }
                    }

                    // 处理一级分类
                    if (!CollectionUtils.isEmpty(secondLevelCategoryVOS)){
                        // 根据parentId 将 secondLevelCategoryVOS 分组
                        Map<Long, List<SecondLevelCategoryVO>> secondLevelCategoryVOMap = secondLevelCategoryVOS.stream().collect(groupingBy(SecondLevelCategoryVO::getParentId));
                        for (SystemSellerCategory firstLevelCategory : firstLevelCategories){
                            WhitesMealIndexCategoryVO whitesMealIndexCategoryVO = new WhitesMealIndexCategoryVO();
                            BeanUtil.copyProperties(firstLevelCategory, whitesMealIndexCategoryVO);
                            // 如果该一级分类下有数据则放入 whitesMealIndexCategoryVOS对象中
                            if (secondLevelCategoryVOMap.containsKey(firstLevelCategory.getCategoryId())){
                                // 根据一级分类的id取出secondLevelCategoryVOMap分组中的二级分类list
                                // 关键: 一级的id对应二级的parentId
                                List<SecondLevelCategoryVO> tempSellerCategories = secondLevelCategoryVOMap.get(firstLevelCategory.getCategoryId());
                                whitesMealIndexCategoryVO.setSecondLevelCategoryVOS(tempSellerCategories);
                                whitesMealIndexCategoryVOS.add(whitesMealIndexCategoryVO);
                            }
                        }
                    }
                }
            }
           return whitesMealIndexCategoryVOS;
        } else {
            return null;
        }
    }

}
